package ivan.bookKeeper;

import java.util.HashMap;
import java.util.Map;

import so.library.AddLibrary;
import util.DialogCreator;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import domain.Library;

public class AddLibraryActivity extends Activity implements OnClickListener {
	private static final String TAG = AddLibraryActivity.class.getSimpleName();
	private static final int DIALOG_ENTER_NAME = 1;
	private static final int DIALOG_ENTER_BORROW_LENGTH = 2;
	private static final int DIALOG_ENTER_ADDRESS = 3;
	private static final int DIALOG_ENTER_EMAIL = 4;
	private static final int DIALOG_ENTER_MNU = 5;
	private static final int DIALOG_ENTER_PHONE = 6;
	private TextView txtLibraryName;
	private TextView txtBorrowLength;
	private ExpandableListView expListAdditionalOptions;
	private BaseExpandableListAdapter expListAdapter; // used by the
														// expandableListView
	// map for holding the values of the user inputs
	private Map<String, Object> map = new HashMap<String, Object>();

	private static TextView[] additionalListTextViews = new TextView[4];

	private String enteredText = "";

	private Library library = null;
	private ImageButton buttonAddLibrary;
	private static DialogCreator dialogCreator = null;

	// //////////////// on create stuff
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_library);

		txtLibraryName = (TextView) findViewById(R.id.libraryName);
		txtLibraryName.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showDialog(DIALOG_ENTER_NAME);
				Log.d(TAG, "Enter Name dialog clicked");
			}
		});

		txtBorrowLength = (TextView) findViewById(R.id.libraryBorrowLength);
		txtBorrowLength.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showDialog(DIALOG_ENTER_BORROW_LENGTH);
				Log.d(TAG, "Borrow length dialog clicked");
			}
		});

		for (int i = 0; i < additionalListTextViews.length; i++) {
			additionalListTextViews[i] = null;
		}

		expListAdditionalOptions = (ExpandableListView) findViewById(R.id.libraryAdditionalOptions);
		expListAdapter = new AdditionalOptionsExpandableListAdapter();
		expListAdditionalOptions.setAdapter(expListAdapter);

		buttonAddLibrary = (ImageButton) findViewById(R.id.buttonAddLibrary);
		buttonAddLibrary.setOnClickListener(this);
	}

	// ////////////// ------- BEGIN dialog stuff ------------//////////////
	@Override
	protected Dialog onCreateDialog(int id) {
		if (dialogCreator == null)
			dialogCreator = new DialogCreator(map, TAG, expListAdapter);
		else if (dialogCreator.getExpListAdapter() == null)
			dialogCreator = new DialogCreator(map, TAG, expListAdapter);
		
		switch (id) {
		case DIALOG_ENTER_NAME:
			return dialogCreator.createDialog(this, getString(R.string.library_name_text)
					+ "!", getString(R.string.library_name_text),
					txtLibraryName, false);
		case DIALOG_ENTER_BORROW_LENGTH:
			return dialogCreator.createDialog(this,
					getString(R.string.library_borrowLength_text) + "!",
					getString(R.string.library_borrowLength_text),
					txtBorrowLength, true);
		case DIALOG_ENTER_ADDRESS:
			return dialogCreator.createDialog(this, getString(R.string.library_address_text)
					+ "!", getString(R.string.library_address_text),
					additionalListTextViews[0], false);
		case DIALOG_ENTER_EMAIL:
			return dialogCreator.createDialog(this, getString(R.string.library_email_text)
					+ "!", getString(R.string.library_email_text),
					additionalListTextViews[1], false);
		case DIALOG_ENTER_MNU:
			return dialogCreator.createDialog(this,
					getString(R.string.library_maxNumExtending_text),
					getString(R.string.library_maxNumExtending_text),
					additionalListTextViews[2], true);
		case DIALOG_ENTER_PHONE:
			return dialogCreator.createDialog(this, getString(R.string.library_phone_text)
					+ "!", getString(R.string.library_phone_text),
					additionalListTextViews[3], false);
		}

		return null;
	}

	

	// /// ---- END DIALOG STUFF ----- // ////

	// // ----- BEGIN EXPANDABLE LIST STUFF ----/////////
	private class AdditionalOptionsExpandableListAdapter extends
			BaseExpandableListAdapter {
		private String[] parents = { "	Additional Options" };
		private String[][] children = { {
				getString(R.string.library_address_text),
				getString(R.string.library_email_text),
				getString(R.string.library_maxNumExtending_text),
				getString(R.string.library_phone_text) } };

		
		private int[][] dialogNumbers = { { DIALOG_ENTER_ADDRESS,
				DIALOG_ENTER_EMAIL, DIALOG_ENTER_MNU, DIALOG_ENTER_PHONE } };

		public AdditionalOptionsExpandableListAdapter()
		{
			if (dialogCreator == null)
				dialogCreator = new DialogCreator(map, TAG, expListAdapter);
		}
		
		@Override
		public Object getChild(int groupPosition, int childPosition) {
			return children[groupPosition][childPosition];
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}

		@Override
		public View getChildView(final int groupPosition,
				final int childPosition, boolean isLastChild, View convertView,
				ViewGroup parent) {
			TextView textView = getGenericView();
			// check if key already exists in map; if not, then set the default
			// text
			if (!dialogCreator.getMap().containsKey((children[groupPosition][childPosition]))) {
				textView.setText(getChild(groupPosition, childPosition)
						.toString());
			}
			// else set value from map as text for TextView
			else {
				String s = (String) dialogCreator.getMap()
						.get(children[groupPosition][childPosition]);
				textView.setText(s);
			}
			textView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					showDialog(dialogNumbers[groupPosition][childPosition]);
					Log.d(TAG, "CLICKED: "
							+ dialogNumbers[groupPosition][childPosition]);
				}
			});
			Log.d(TAG, "CHILD NUMBER IS: " + childPosition);
			additionalListTextViews[childPosition] = textView;
			return additionalListTextViews[childPosition];
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			return children[groupPosition].length;
		}

		@Override
		public Object getGroup(int groupPosition) {
			return parents[groupPosition];
		}

		@Override
		public int getGroupCount() {
			return parents.length;
		}

		@Override
		public long getGroupId(int groupPosition) {
			return groupPosition;
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			TextView textView = getGenericView();
			textView.setText(getGroup(groupPosition).toString());
			return textView;
		}

		@Override
		public boolean hasStableIds() {
			return true;
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return true;
		}

		public TextView getGenericView() {
			// Layout parameters for the ExpandableListView
			AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
					ViewGroup.LayoutParams.MATCH_PARENT, 64);

			TextView textView = new TextView(AddLibraryActivity.this);
			textView.setLayoutParams(lp);
			// Center the text vertically
			textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
			// Set the text starting position
			textView.setPadding(36, 0, 0, 0);
			// textView.setGravity(Gravity.CENTER);
			// textView.setBackgroundDrawable(getResources().getDrawable(R.drawable.back));
			return textView;
		}

		@Override
		public void notifyDataSetChanged() {
			// TODO Auto-generated method stub
			super.notifyDataSetChanged();
		}
	}

	// //----- END EXPANDABLE LIST STUFF ----/////////

	// ////////////// add button stuff
	@Override
	public void onClick(View v) {
		String libraryName = null, libraryAddress = null, libraryPhone = null, libraryEmail = null;
		int maxNumExtending = 0, borrowLength = 0;
		map = dialogCreator.getMap();
		if (map.isEmpty()) {
			showLongToast(getString(R.string.toastMessage_error_noInput));
		} else {
			// /// check for library's name
			if (map.containsKey(getString(R.string.library_name_text))) {
				libraryName = (String) map
						.get(getString(R.string.library_name_text));
			} else {
				showLongToast(getString(R.string.toastMessage_error_general)
						+ getString(R.string.library_name_text));
				return;
			}

			// // check for library's borrow max
			if (map.containsKey(getString(R.string.library_borrowLength_text))) {
				borrowLength = Integer.parseInt((String) map
						.get(getString(R.string.library_borrowLength_text)));
			} else {
				showLongToast(getString(R.string.toastMessage_error_general)
						+ getString(R.string.library_borrowLength_text));
				return;
			}

			// // check for library's maxNumExt
			if (map.containsKey(getString(R.string.library_maxNumExtending_text))) {
				maxNumExtending = Integer.parseInt((String) map
						.get(getString(R.string.library_maxNumExtending_text)));
			}

			// // check for library's address
			if (map.containsKey(getString(R.string.library_address_text))) {
				libraryAddress = (String) map
						.get(getString(R.string.library_address_text));
			}

			// // check for library's email
			if (map.containsKey(getString(R.string.library_email_text))) {
				libraryEmail = (String) map
						.get(getString(R.string.library_email_text));
			}

			// // check for library's phone
			if (map.containsKey(getString(R.string.library_phone_text))) {
				libraryPhone = (String) map
						.get(getString(R.string.library_phone_text));
			}

			// // create new library object
			library = new Library(borrowLength, maxNumExtending, libraryName,
					libraryAddress, libraryPhone, libraryEmail);

			new AddLibrary(AddLibraryActivity.this).execute(library);
		}
	}

	private void showLongToast(String text) {
		Toast.makeText(AddLibraryActivity.this, text, Toast.LENGTH_LONG).show();
	}
}
